package com.zzb.service.impl;

import com.zzb.DTO.CompanyDTO;
import com.zzb.DTO.ResultDTO;
import com.zzb.VO.CompanyVO;
import com.zzb.dao.CompanyDao;
import com.zzb.dao.impl.CompanyDaoImpl;
import com.zzb.entity.CompanyEntity;
import com.zzb.service.CompanyService;
import com.zzb.util.Tool;
import org.springframework.beans.BeanUtils;

import java.util.List;

public class CompanyServiceImpl implements CompanyService {
    CompanyEntity companyEntity = new CompanyEntity();
    CompanyDao companyDao = new CompanyDaoImpl();

    @Override
    public ResultDTO add(CompanyVO companyVO) {
        //数据复制
        BeanUtils.copyProperties(companyVO, companyEntity);
        if(companyDao.add(companyEntity)==1){
            return ResultDTO.getResult("success",1);
        }else {
            return ResultDTO.getResult("error",0);
        }
    }

    @Override
    public ResultDTO delete(CompanyVO companyVO) {
        //数据复制
        BeanUtils.copyProperties(companyVO, companyEntity);
        //判断信息是否存在
        if (companyDao.findById(companyEntity) != null) {//存在
            return ResultDTO.getResult("success", companyDao.delete(companyEntity));
        } else {//不存在
            return ResultDTO.getResult("error", 0);
        }
    }

    @Override
    public ResultDTO findById(CompanyVO companyVO) {
        BeanUtils.copyProperties(companyVO, companyEntity);
        companyEntity = companyDao.findById(companyEntity);
        if (companyEntity != null) {
            CompanyDTO companyDTO = new CompanyDTO();
            BeanUtils.copyProperties(companyEntity, companyDTO);
            return ResultDTO.getResult("success", companyDTO);
        } else {
            return ResultDTO.getResult("error", 0);
        }
    }

    @Override
    public ResultDTO findAll(CompanyVO companyVO) {
        BeanUtils.copyProperties(companyVO,companyEntity);
        int count = companyDao.count(companyEntity);
        List<CompanyEntity> list = companyDao.findAll(companyEntity);
        //集合复制
        List<CompanyDTO> companyDTOList = Tool.copy(list, CompanyDTO.class);
        return ResultDTO.getResult(count, companyDTOList);
    }

    @Override
    public ResultDTO update(CompanyVO companyVO) {
        BeanUtils.copyProperties(companyVO, companyEntity);
        return ResultDTO.getResult(companyDao.update(companyEntity));
    }
}
